//获取应用实例
const request = require('../../../../utils/request');
const app = getApp()
//请求接口函数
function getOneOrder(that,id,order_type) {
  request.HttpRequst(true, 'user/orderDetail', false, '', { "token": wx.getStorageSync("sessionId"), "id":id, "order_type":order_type}, "post", false, res => {
    console.log(res)
    if (res.code == 200) {//返回有数据
      let price = 0;
      res.data.room_table.forEach(  res => {
        price += Number(res.min_consumption)
      });
      if(res.data.order_status == 3) {
        app.globalData.isCallWaiter = true
      }else {
        app.globalData.isCallWaiter = false
      }
      that.setData({
        OneOrder:res.data,
        order: res.data,
        min_consumption: price
      })
    } else {//返回错误（弹窗提示）
      if(res.info != 'empty'){
        wx.showModal({
          title: '提示',
          content: res.info,
        })
      }
    }
  })
}
//页面
Page({
    //初始化数据
    data:{
        id: '',
        order_type:'',
        OneOrder:'',
        order:{},  //订单数据
        token: wx.getStorageSync("sessionId"),
        anginOrderDish: {},
        foods: {},
        template: 0,
        isInvitation: true,
        min_consumption: 0,
        canPay: true, //点的菜是否到达桌子的最低消费
    }, 
    //获取链接参数
    onLoad: function (e) {
      this.setData({
        id: e.id,
        order_type: e.order_type
      })
      var that = this
      console.log('zhshiID'+that.data.id)
      getOneOrder(that,that.data.id,that.data.order_type)
      request.HttpRequst(true,'showinvitation',false,'',{'order_id':e.id},"POST",false,res => {
        console.log('邀请函：',res)
          if(res.code == 200){
              this.setData({
                template: res.data.templet
              })
          }else{
            this.setData({
              isInvitation:false
            })
          }
      })
    },
    onReady: function(){
        wx.setNavigationBarTitle({
            title: '订单详情'
        })
    },


    //外卖操作
    //申请退款
    TapplyForRefund() {
      request.HttpRequst(true, 'TapplyForRefund', false, '', { "token": wx.getStorageSync("sessionId"), "id":this.data.id}, "post", false, res => {
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: res.info,
            success: function() {
              wx.navigateBack({     //返回上一页面或多级页面
                delta: 1
              })
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.info
          })
        }
      })
    },

    //确认收货
    confirmReceipt() {
      request.HttpRequst(true, 'confirmReceipt', false, '', { "token": wx.getStorageSync("sessionId"), "id":this.data.id}, "post", false, res => {
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: res.info,
            success: function() {
              wx.navigateBack({     //返回上一页面或多级页面
                delta: 1
              })
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.info
          })
        }
      })
    },

    //预定操作

    //预定退款
    applyForRefund() {
      request.HttpRequst(true, 'applyForRefund', false, '', { "token": wx.getStorageSync("sessionId"), "id":this.data.id}, "post", false, res => {
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: res.info,
            success: function() {
              wx.navigateBack({     //返回上一页面或多级页面
                delta: 1
              })
            }
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.info
          })
        }
      })
    },
    //我已到店
    arrivaledShop() {
    let that = this
      wx.showModal({
        title: '提示',
        content: '您真的到店了吗，点确定后服务员马上上菜',
        success: function(res) {
          if (res.confirm) {
            request.HttpRequst(true, 'arrivaledShop', false, '', { "token": wx.getStorageSync("sessionId"), "id":that.data.id}, "post", false, res => {
              console.log(res)
              if(res.code == 200){
                wx.showModal({
                  title: '提示',
                  content: res.info,
                  success: function() {
                    wx.navigateBack({     //返回上一页面或多级页面
                      delta: 1
                    })
                  }
                })
              }else{
                wx.showModal({
                  title: '提示',
                  content: res.info
                })
              }
            })
          }else if(res.cancel){
            console.log("取消")
          }
          
        },
        fail: function() {
          
        }
      })
      
    },
    //呼叫服务员
    callAttendant(e) {
      let desk_id = e.currentTarget.dataset.roomid
      request.HttpRequst(true, 'callAttendant', false, '', { "token": wx.getStorageSync("sessionId"), "id":this.data.id,'desk_id':desk_id}, "post", false, res => {
        console.log(res)
        if(res.code == 200){
          wx.showModal({
            title: '提示',
            content: res.info,
            showCancel: false
          })
        }else{
          wx.showModal({
            title: '提示',
            content: res.info,
            showCancel: false
          })
        }
      })
    },

    //去支付
    pay(e) {
      console.log(e)
      let price = 0
      price = this.data.OneOrder.order_amount;
      if(e.currentTarget.dataset.type == '预定订单'){
        price = Math.floor((this.data.OneOrder.order_price - this.data.OneOrder.order_amount)*100)/100
      }
      
      wx.navigateTo({
        url: '../../../index/pay/pay?order_id='+e.currentTarget.dataset.id+'&price='+price+'&type='+e.currentTarget.dataset.type
      })
    },

    //删除订单
    deleteOrder() {
      let that = this;
      console.log(123123)
      wx.showModal({
        title: '提示',
        content: '确定要删除该订单？',
        success: function(res) {
          if (res.confirm) {
            request.HttpRequst(true, 'program/cancleNotPayOrder', false, '', { "token": wx.getStorageSync("sessionId"), "id":that.data.id}, "post", false, res => {
              console.log(res)
              wx.navigateBack({     //返回上一页面或多级页面
                delta: 1
              })
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
    },

    //加菜
    addDishes(e){
      console.log(this.data.order)
      let desk_id = e.currentTarget.dataset.roomid
      let allData = {};
      let order = this.data.order;
      let selectRoom = [];
      let min_consumption = 0;
      order.room_table.forEach( res => {
        selectRoom.push(res.id+','+res.min_consumption+','+res.name);
        min_consumption += Number(res.min_consumption)
      })
      allData = {
        'name': order.order_reserve.name,
        'phone': order.order_reserve.phone,
        'desk_room': order.order_reserve.desk_room,
        'arrive_time': order.order_reserve.arrive_time,
        'shop_id': order.shop_id,
        'status': 'addDish',
        'order_id': order.id,
        'selectRoom': selectRoom,
        'min_consumption': min_consumption,
        'desk_room_id': desk_id
      }
      console.log(allData)
      wx.navigateTo({
        url: '../../../index/list/advance/advList/advList?data='+JSON.stringify(allData)
      })
    },

    //外卖再来一单
    anginOrder(e) {
      let id = e.currentTarget.dataset.id
      request.HttpRequst(true, 'program/nextOrder', false, '', { "token": this.data.token, "id":id}, "post", false, res => {
        console.log(res)
        if(res.code == 200){
          this.setData({
            anginOrderDish: res.data,
            foods:res.data.detail
          })
          // wx.navigateTo({
          //   url:'../../../index/shop/shopOrder/shopOrder?status=angin&id='+res.data.shop_id
          // })
          wx.navigateTo({
              url:'../../../index/shop/shop?id='+res.data.shop_id
          })
        }
        console.log(this.data.anginOrderDish)
      })
    },

    //查看邀请函
    seeInvitation() {
      if(this.data.template == 0){
        wx.navigateTo({
          url: '../../../../pagesA/invitation/commonModel/commonModel?order_id='+this.data.id
        })
      }else{
        wx.navigateTo({
          url: '../../../../pagesA/invitation/birthdayModel/birthdayModel?order_id='+this.data.id
        })
      }
    },

    //查看预定商家
    goAdvShop(e) {
      console.log(123123)
      let shop_id = e.currentTarget.dataset.shop_id;
      wx.navigateTo({
        url: '../../../index/list/advance/shop/shop?id='+shop_id
      })
    }
})